﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System.IO;
using System.Net.Http.Formatting;

namespace MSTutHelloWebAPI.Models
{
    public class TextAnalytics
    {
        private ScriptEngine engine;
        private ScriptScope scope;
        private ObjectOperations ops;
        private dynamic pythonType;
        private dynamic instance;

        public TextAnalytics()
        {
            try
            {
                engine = Python.CreateEngine();

                ICollection<string> paths = engine.GetSearchPaths();
                paths.Add(@"C:\Program Files (x86)\IronPython 2.7\lib");
                paths.Add(@"C:\Program Files (x86)\IronPython 2.7\lib\site-packages\setuptools-0.6c11-py2.7.egg");
                paths.Add(@"C:\Program Files (x86)\IronPython 2.7\lib\site-packages\pip-1.3.1-py2.7.egg");
                paths.Add(@"C:\ws\Python27\Lib\site-packages\pyyaml-3.10-py2.7-win-amd64.egg\");
                paths.Add(@"C:\Program Files (x86)\IronPython 2.7\lib\site-packages\nltk-2.0.4-py2.7.egg");
                engine.SetSearchPaths(paths);

                scope = engine.CreateScope();
                ops = engine.Operations;

                engine.ExecuteFile("Program2.py", scope);
                Load();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Oops! There was an exception" +
                  " while running the script: " + ex.Message);
                throw new Exception("********************** Unable to instantiate Text Analytics " + ex.Message);
            }
        }

        public int TestLoad()
        {
            if (instance == null) return -1;
            return 1;
        }

        private void Load()
        {
            try
            {
                pythonType = scope.GetVariable("MyClass");
                instance = ops.CreateInstance(pythonType);
                instance.load();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Oops! There was an exception" +
                  " while running the script: " + ex.Message);
                throw new Exception("********************* Unable to Load Instance" + ex.Message);
            }
        }

        public string Go(string input)
        {
            MemoryStream ms = new MemoryStream();
            engine.Runtime.IO.SetOutput(ms, new StreamWriter(ms));
            instance.go(input);
            string result = ReadFromStream(ms);
            return result;
            //return "Hello";
        }

        private static string ReadFromStream(MemoryStream ms)
        {
            int length = (int)ms.Length;
            Byte[] bytes = new Byte[length];

            ms.Seek(0, SeekOrigin.Begin);
            ms.Read(bytes, 0, (int)ms.Length);

            return Encoding.GetEncoding("utf-8").GetString(bytes, 0, (int)ms.Length);
        }
    }
}